package com.hileto.inf.utils.spring;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Copyright: Copyright (c) 2012
 *     深圳市海乐淘电子商务有限公司
 * @Author : 曾林宝
 * @CreateTime : 2013-1-21 上午10:46:40
 *
 * @Description: HESSIAN SERVLET代理类
 */
public class SpringHessianProxy extends GenericServlet {
	private static final long serialVersionUID = 5403602222473813270L;

	private String targetBean;
	private HessianServiceExporter proxy;

	public void init() throws ServletException {
		this.targetBean = this.getServletName();
		getServletBean();
	}

	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		proxy.handleRequest(request, response);
	}

	private void getServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		this.proxy = (HessianServiceExporter) wac.getBean(targetBean);
	}
}
